<template>
  <basic-container :btnFlag=true>
    <div class="card-border">
      <card-header :header-data="headerData"></card-header>
      <el-steps :active="1" simple style="margin-top: 10px">
        <el-step title="基本信息入库" icon="el-icon-edit"></el-step>
        <el-step title="相关课题入库" icon="el-icon-upload"></el-step>
        <el-step title="相关报告入库" icon="el-icon-picture"></el-step>
      </el-steps>
      <!--<div style="font-weight:600;margin-top: 20px;">相关课题信息:</div>-->
      <div style="float: right;margin:19px 5px 9px 0px">
        <el-button type="success" size="mini" @click="addSubjectResult()">添加相关课题</el-button>
      </div>
      <avue-crud :data="subjectResultListData"
                 :option="subjectResultOption"
                 :page="subjectResultPage"
                 :table-loading="tableLoading"
                 @on-load="getSubjectResultList"
                 @size-change="sizeChange"
                 @current-change="currentChange"
                 class="avuecrud">
        <template slot-scope="scope" slot="menu">
          <div class="menu-title" @click.stop="editSubjectResult(scope.row,scope.index)">
            <i class="iconfont iconalter icon-warning"></i>
            <p class="menu-title-text">修改</p>
          </div>
        </template>
      </avue-crud>
      <div style="text-align: center;margin-top: 30px">
        <el-button type="primary" @click="beforeStep()">上一步</el-button>
        <el-button @click="goBack()">关 闭</el-button>
        <el-button type="primary" @click="nextStep()">下一步</el-button>
      </div>

      <!--添加相关课题-->
      <el-dialog :title="addSubjectResultTitle" :visible.sync="addSubjectResultVisible" @close='close' @closed='closed'
                 width="60%" :key="addSubjectResultKey">
        <el-form label-width="130px"
                 :model="subjectForm"
                 style="padding-right: 30px;"
                 :rules="subjectFormRules"
                 ref="subjectForm">
          <el-row style="margin-top: 18px;">
            <el-col :span="20">
              <el-form-item class="form-item"
                            label="课题名称:"
                            prop="title">
                <el-input v-model="subjectForm.title"
                          placeholder="请输入课题名称">
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="20">
              <el-form-item class="form-item"
                            label="课题概述:"
                            prop="description">
                <el-input v-model="subjectForm.description"
                          type="textarea"
                          :autosize="{ minRows: 3, maxRows: 4}"
                          placeholder="请输入课题概述">
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="16">
              <el-form-item label="牵头部门 :" prop="deptName" class="form-item">
                <span style="font-size: 16px">{{subjectForm.deptName}}</span>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-button size="small" type="primary" icon="el-icon-plus" style="margin-left: 10px;"
                         @click="clickMainDept">选择牵头部门
              </el-button>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="16">
              <el-form-item label="配合部门 :" prop="supportDeptName" class="form-item">
                <span style="font-size: 16px">{{subjectForm.supportDeptName}}</span>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-button size="small" type="primary" icon="el-icon-plus" style="margin-left: 10px;"
                         @click="clickAssortDept">选择配合部门
              </el-button>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="22">
              <el-form-item class="form-item"
                            label="课题成果文件:"
                            prop="resultFileId"
                            :required=true>
                <el-upload class="upload-demo"
                           ref="upload"
                           :limit="1"
                           :on-exceed="tipFileOne"
                           action="string"
                           :http-request="submitUploadtextMixin"
                           :on-preview="handlePreviewtextMixin"
                           :on-remove="handleRemovetextMixin"
                           :file-list="fileListtextMixin"
                           :auto-upload="true"
                           :on-change="fileChangetextMixin">
                  <el-button slot="trigger"
                             size="small"
                             type="primary">点击上传
                  </el-button>
                </el-upload>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="22">
              <el-form-item class="form-item"
                            label="相关材料:"
                            prop="content">
                <el-upload class="upload-demo"
                           ref="uploadrelated"
                           action="string"
                           :http-request="submitUploadMixin"
                           :on-preview="handlePreviewMixin"
                           :on-remove="handleRemoveMixin"
                           :file-list="fileListMixin"
                           :auto-upload="true"
                           :on-change="fileChangeMixin">
                  <el-button slot="trigger"
                             size="small"
                             type="primary">点击上传
                  </el-button>
                </el-upload>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row style="font-weight: 600">
            <el-col :span="20">
              <el-form-item class="form-item" label="共享范围 :" prop="openType">
                <el-select
                  v-model="subjectForm.openType"
                  placeholder="请选择共享范围"
                  style="width: 280px"
                >
                  <el-option
                    v-for="item in openTypeDic"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row v-if="subjectForm.openType=='1'" style="font-weight: 600">
            <el-col :span="18">
              <el-col :span="14">
                <el-form-item class="form-item" label="共享部门 :" prop="openDeptNames">
                  {{subjectForm.openDeptNames}}
                </el-form-item>
              </el-col>
              <el-col :span="10">
                <el-button size="small" type="primary" icon="el-icon-plus" style="margin-left: 10px;"
                           @click="clickOpenDept">部门
                </el-button>
              </el-col>
            </el-col>
          </el-row>
        </el-form>
        <div style="text-align: center;">
          <span slot="footer"
                class="dialog-footer">
             <el-button type="primary"
                        :disabled="isDisabled"
                        @click="commitSubjectResult()"
                        v-if="addOrEdit">确 定</el-button>
            <el-button type="primary"
                       :disabled="isDisabled"
                       @click="commitSubjectResultEdit()"
                       v-if="!addOrEdit">确定修改</el-button>
            <el-button @click="cancel()">取 消</el-button>
          </span>
        </div>
      </el-dialog>
      <!--牵头单位部门选择-->
      <dept-select :visible="deptVisible"
                   @getSelectInfo="getSelectInfo"
                   @cancel="cancelDept"
                   :miniType="'2'"></dept-select>
      <dept-check :visible="supportDeptVisible" @getCheckInfo="getSupportDeptCheckInfo"
                  @cancel="cancelSupportDept" :miniType="'2'"></dept-check>
      <dept-check :visible="openDeptVisible" @getCheckInfo="getOpenDeptCheckInfo"
                  @cancel="cancelOpenDept"></dept-check>
    </div>
  </basic-container>
</template>

<script>
  import textfile from "@/mixins/textfile";
  import relatedfile from "@/mixins/relatedfile";
  import {subjectResultFormRules} from "@/const/infoRules";
  import {addSubjectResult, getSubjectList, getSubjectDetail, editSubjectResult} from "@/api/subject";
  import {getFileTypeFiles} from '@/views/util'

  export default {
    name: "resultAddSubject",
    mixins: [textfile, relatedfile],
    created() {
      this.subjectForm.achievementId = this.$route.params.id;
    },
    data() {
      return {
        headerData: {
          title: '成果入库'
        },
        tableLoading: false,
        subjectResultListData: [],
        subjectResultPage: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 10 // 每页显示多少条
        },
        subjectResultOption: {
          page: true,
          menuWidth: 100,
          addBtn: false,
          editBtn: false,
          addRowBtn: false,
          indexLabel: "序号",
          index: true,
          cellBtn: false,
          indexWidth:70,
          delBtn: false,
          columnBtn: false,
          refreshBtn: false,
          filterBtn: false,
          searchBtn: false,
          keyId: "id",
          column: [

            {
              label: "课题名称",
              prop: "title",
              cell: true
            },
            {
              label: "课题概述",
              prop: "description",
              overHidden: true,
              cell: true
            },
            {
              label: "牵头部门",
              width: 120,
              prop: "deptName",
              cell: true
            },
            {
              label: "配合部门",
              width: 150,
              prop: "supportDeptName",
              cell: true
            },
          ]
        },
        addSubjectResultVisible: false,
        subjectFormRules: subjectResultFormRules,
        subjectForm: {
          title: '',
          description: '',
          achievementId: null,
          deptName: '',
          deptId: null,
          supportDeptName: '',
          supportDeptIds: '',
          openType: '0',
          openDeptNames: '',
          openDeptIds: [],
          resultFileId: null,
          annexFileIds: [],
          subjectId: null,
        },
        resetSubjectForm: {
          title: '',
          description: '',
          achievementId: null,
          deptName: '',
          deptId: null,
          supportDeptName: '',
          supportDeptIds: '',
          openType: '0',
          openDeptNames: '',
          openDeptIds: [],
          resultFileId: null,
          annexFileIds: [],
          subjectId: null,
        },
        deptVisible: false,//协同部门树弹窗标志
        supportDeptVisible: false,//协同部门树弹窗标志
        openDeptVisible: false,//部分部门共享树弹窗标志
        projectForm: {
          textId: null,
        },
        isDisabled: false,
        openTypeDic: [
          {
            label: '全共享',
            value: '0',
          },
          {
            label: '不共享',
            value: '2',
          },
          {
            label: '部分部门共享',
            value: '1',
          },
        ],
        addSubjectResultTitle: '添加相关课题',
        addOrEdit: true,
        addSubjectResultKey: 1,

      }
    },
    methods: {
      getSubjectResultList() {
        getSubjectList(Object.assign({
          current: 1,
          size: 10,
          achievementId: this.$route.params.id,
          states: '9',
        })).then(response => {
          this.subjectResultListData = response.data.data.records;
          this.subjectResultPage.total = response.data.data.total;
        })
      },
      getSubjectResultListPage(page) {
        getSubjectList(Object.assign({
          current: page.currentPage,
          size: page.pageSize,
          achievementId: this.$route.params.id,
          states: '9',
        })).then(response => {
          this.subjectResultListData = response.data.data.records;
          this.subjectResultPage.total = response.data.data.total;
        })
      },
      currentChange(currentPage) {
        var page = this.subjectResultPage;
        page.currentPage = currentPage;
        this.getSubjectResultListPage(page);
      },
      sizeChange(pageSize) {
        var page = this.subjectResultPage;
        page.pageSize = pageSize;
        this.getSubjectResultListPage(page);
      },
      editSubjectResult(row) {
        this.addOrEdit = false;
        this.addSubjectResultVisible = true;
        this.addSubjectResultKey = this.addSubjectResultKey + 1;
        this.addSubjectResultTitle = '修改相关课题';
        getSubjectDetail(row.subjectId).then(response => {
          let records = response.data.data;
          //避免指针赋值使的openType元素不见了
          this.subjectForm.title = records.title;
          this.subjectForm.description = records.description;
          this.subjectForm.achievementId = records.achievementId;
          this.subjectForm.deptName = records.deptName;
          this.subjectForm.deptId = records.deptId;
          this.subjectForm.supportDeptName = records.supportDeptName;
          this.subjectForm.supportDeptIds = records.supportDeptIds;
          this.subjectForm.subjectId = records.subjectId;


          let resultFileList = response.data.data.resultFileList;
          if (resultFileList != null && resultFileList.length != 0) {
            this.subjectForm.resultFileId = resultFileList[0].fileId;
            this.projectForm.textId = resultFileList[0].fileId;
            this.fileListtextMixin = [{name: resultFileList[0].fileFullName, url: resultFileList[0].fileId}];

            if (resultFileList[0].annexFileIds != null && resultFileList[0].annexFileIds.length != 0) {
              let annexFileIds = resultFileList[0].annexFileIds;
              this.subjectForm.annexFileIds = annexFileIds;
              this.setFilesList(annexFileIds);
            }
            //共享类型
            this.subjectForm.openType = resultFileList[0].openType;
            this.subjectForm.openDeptIds = resultFileList[0].openDeptIds;
            this.subjectForm.openDeptNames = resultFileList[0].openDeptNames;
          }

        })
      },
      addSubjectResult() {
        this.addOrEdit = true;
        this.addSubjectResultVisible = true;
        this.addSubjectResultKey = this.addSubjectResultKey + 1;
        this.addSubjectResultTitle = '添加相关课题';
      },
      commitSubjectResult() {
        var arr = [];
        if (this.fileListMixin.length != 0) {
          for (var item in this.fileListMixin) {
            arr.push(this.fileListMixin[item].url);
          }
          this.subjectForm.annexFileIds = arr;
        }
        this.subjectForm.resultFileId = this.projectForm.textId;


        if (this.subjectForm.openDeptIds != null && this.subjectForm.openDeptIds!= '') {
          this.subjectForm.openDeptIds = this.subjectForm.openDeptIds.split(',');
        }
        this.$refs.subjectForm.validate(valid => {
          if (valid) {
            this.$confirm("确定添加此课题吗?", "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning"
            }).then(() => {
              this.subjectForm.achievementId = this.$route.params.id;
              addSubjectResult(this.subjectForm).then(response => {
                if (response.data.code == 0) {
                  this.$notify({
                    title: "成功",
                    message: "发送成功",
                    type: "success",
                    duration: 2000
                  });
                  this.getSubjectResultListPage(this.subjectResultPage);
                  this.addSubjectResultVisible = false;
                }
              })
            })
          }
        })
      },
      commitSubjectResultEdit() {
        var arr = [];
        if (this.fileListMixin.length != 0) {
          for (var item in this.fileListMixin) {
            arr.push(this.fileListMixin[item].url);
          }
          this.subjectForm.annexFileIds = arr;
        }

        this.subjectForm.resultFileId = this.projectForm.textId;
        this.$refs.subjectForm.validate(valid => {
          if (valid) {
            this.$confirm("确定修改此课题吗?", "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning"
            }).then(() => {
              this.subjectForm.achievementId = this.$route.params.id;
              editSubjectResult(this.subjectForm.subjectId, this.subjectForm).then(response => {
                if (response.data.code == 0) {
                  this.$notify({
                    title: "成功",
                    message: "修改成功",
                    type: "success",
                    duration: 2000
                  });
                  this.getSubjectResultListPage(this.subjectResultPage);
                  this.addSubjectResultVisible = false;
                }
              })
            })
          }
        })
      },
      closed() {
        this.subjectForm = this.deepClone(this.resetSubjectForm);
        this.fileListMixin = [].concat();
        this.fileListtextMixin = [].concat();
        this.projectForm.textId = null;
      },
      close() {
        this.subjectForm = this.deepClone(this.resetSubjectForm);
        this.fileListMixin = [].concat();
        this.fileListtextMixin = [].concat();
        this.projectForm.textId = null;
      },
      cancel() {
        this.addSubjectResultVisible = false;
      },
      async setFilesList(fileIds) {
        let result = await getFileTypeFiles(fileIds);
        if (result != null && result.length != 0) {
          for (let item in result) {
            this.fileListMixin.push({name: result[item].fileName, url: result[item].fileId})
          }
        }

      },
      //转到下一步
      nextStep() {
        let src = `/resultAddReport/detail/${this.$route.params.id}`;
        this.$router.push({
          path: src
        })
      },
      // 弹出选择牵头部门弹框
      clickMainDept() {
        this.deptVisible = true
      },
      cancelDept(deptIds, deptNames) {
        this.deptVisible = false;
      },
      getSelectInfo(deptId, deptName) {
        this.subjectForm.deptName = deptName;
        this.subjectForm.deptId = deptId;
        this.deptVisible = false;
      },
      //弹出选择配合部门弹框
      clickAssortDept() {
        this.supportDeptVisible = true;
      },
      cancelSupportDept() {
        this.supportDeptVisible = false;
      },
      getSupportDeptCheckInfo(deptIds, deptNames) {
        this.subjectForm.supportDeptName = deptNames;
        this.subjectForm.supportDeptIds = deptIds;
        this.supportDeptVisible = false;
      },
      //弹出选择部分部门共享弹框
      clickOpenDept() {
        this.openDeptVisible = true;
      },
      cancelOpenDept() {
        this.openDeptVisible = false;
      },
      getOpenDeptCheckInfo(deptIds, deptNames) {
        this.subjectForm.openDeptNames = deptNames;
        this.subjectForm.openDeptIds = deptIds;
        this.openDeptVisible = false;
      },
      // 返回首页
      goBack() {
        this.$router.push({path: "/wel"});
      },
      beforeStep() {
        let src = `/resultEdit/detail/${this.$route.params.id}`;
        this.$router.push({
          path: src
        })
      },


    },
  }
</script>

<style lang="scss" scoped>
  .card-border {
    border: 1px solid #d2d2d2;
    padding: 12px;
  }
</style>
